/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biofilter.pipelines;

import biofilter.filters.CompositeFilter;
import biofilter.sources.RecordsFile;
import biofilter.filters.HistogramFilter;
import biofilter.filters.RecordsFile2BowtieRecord;
import biofilter.util.SystemProperties;
import biofilter.records.*;
import biofilter.util.ArrayUtil;
import biofilter.util.ProcessUtil;
import java.io.File;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Daniel Quest
 * @date   Dec 31 2009
 */
public class BowtieHistoPipeline extends PipelineBase {

    private RecordsFile fnrf = null;
    private HistogramFilter histoF = null;
    private RecordsFile2BowtieRecord rf2br = null;
    private ProcessUtil pu = new ProcessUtil();
    private SystemProperties sp = new SystemProperties();
    public BowtieHistoPipeline (String fileNameRecordsFile) throws Exception {
            super();
            this.fnrf = new RecordsFile(fileNameRecordsFile,
                                               "biofilter.records.FileNameRecord");
            init();
    }

    public BowtieHistoPipeline (RecordsFile fnrf) throws Exception {
            super();        
            this.fnrf = fnrf;
            init();
    }

    private void init(){
        try {
            this.filters.setSource(fnrf);
            histoF = new HistogramFilter();
            rf2br = new RecordsFile2BowtieRecord();
            this.filters.addFilter(rf2br);
            this.filters.addFilter(histoF);
            this.outputDir = pu.getOutputDir(sp.get("tmp"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(BowtieHistoPipeline.class.getName()).log(Level.SEVERE, "tmp not correctly formatted in sys.properties", ex);
        }
    }
    /**
     * Call requireUnique if reads must have one and only one match in-order to be mapped
     */
    public void requireUnique(){
        histoF.unique();
    }

    private String outputDir = null;
    private String targetDatabase = "";

    public String getOutputDir() {
        return outputDir;
    }

    public void setOutputDir(String outputDir) {
        if (!outputDir.endsWith("/")) outputDir = outputDir + "/";
        this.outputDir = outputDir;
    }

    public String getTargetDatabase() {
        return targetDatabase;
    }

    public void setTargetDatabase(String targetDatabase) {
        this.targetDatabase = targetDatabase;
    }

    @Override
    public Vector<Record> execPipeline() throws Exception {
        Vector<Record> resultFiles = new Vector<Record>();
        while (this.filters.eof() != true) {
            HistogramRecord hr = (HistogramRecord) this.filters.getRecord();
            String outfile = this.outputDir + pu.getUniqueFileName(targetDatabase + ".hist");
            Logger.getLogger(BowtieMapperPipeline.class.getName()).log(Level.INFO, "Printing Histogram to: " + outfile);
            hr.toFile(outfile);
            //System.out.println( hr.get() );
//            resultFiles.add(this.recordToFile(hr, hr.getTarget(), ".histo"));
        }
        return resultFiles;
    }

    public void main(String[] args) {
        try {
            BowtieHistoPipeline bthp = new BowtieHistoPipeline(args[0]);
            bthp.execPipeline();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

}
